package com.qiangqiang.task.server.launch;


import com.qiangqiang.task.server.mapper.UserMapper;
import com.qiangqiang.task.server.ranking.RankingTree;
import com.qiangqiang.task.server.ranking.entity.RankingUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: task
 * @description:
 * @author: Sky.lin
 * @create: 2019-02-13 01:38
 **/
@Component
@Order(1)
public class InitRankingTree implements ApplicationRunner {

    @Autowired
    RankingTree rankingTree;

    @Autowired
    UserMapper userMapper;


    @Override
    public void run(ApplicationArguments args) throws Exception {

        //初始化线段树，成就值区间为[0,2000]
        rankingTree.setup(0, 5000);

        //从数据库中获取所有的用户
        userMapper.queryAllUser()
                .stream()
                .forEach(e -> {
                    RankingUser rankingUser = new RankingUser();
                    BeanUtils.copyProperties(e, rankingUser);
                    // 插入到线段树中
                    rankingTree.insert(rankingUser.getAchievement(), rankingUser);
                });
        System.out.println("初始化完成");
    }

}
